<template>
  <div class="adevice-collectparams">
    <h2>{{selectRow.name}} 采集参数:</h2>
    <Form :model="formData" :label-width="100" :rules="formRules" ref="collectparamsRef">
      <FormItem label="采集频率:" prop="rate">
        <Input v-model.trim="formData.rate" :disabled="disabledRate"></Input>
      </FormItem>
      <template v-if="showDom">
        <FormItem label="CT:" prop="ct">
          <Input v-model.trim="formData.ct"></Input>
        </FormItem>
        <FormItem label="PT:" prop="pt">
          <Input v-model.trim="formData.pt"></Input>
        </FormItem>
        <FormItem label="LCT:" prop="lct">
          <Input v-model.trim="formData.lct"></Input>
        </FormItem>
      </template>
      <FormItem style="text-align:center;">
        <Button @click.stop.prevent="saveParams">保存</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import { validateDeviceId } from "@/utils/rules"
import {
  getcollectorParams,
  setcollectorParams,
  savecollectorParams,
  getDeviceDetail,
  getDeviceType,
  getDeviceTypeSeries
} from "@/api"
export default {
  mounted() {
    this.getInitialState().then(res => {
      // this.getDeviceTypeList()
      this._getcollectorParams()
    })
  },
  data() {
    return {
      selectRow: {},
      isSave: false,
      formData: {
        id: "",
        rate: "",
        ct: "",
        pt: "",
        lct: "",
        deviceId: ""
      },
      disabledRate: false,
      deviceTypes: [],
      showDom: false,
      formRules: {
        rate: [
          {
            required: true,
            trigger: "blur",
            validator: validateDeviceId("采集频率", "采集频率只能输入正整数")
          }
        ],
        ct: [
          {
            required: true,
            trigger: "blur",
            validator: validateDeviceId("CT", "CT值只能输入正整数")
          }
        ],
        pt: [
          {
            required: true,
            trigger: "blur",
            validator: validateDeviceId("PT", "PT值只能输入正整数")
          }
        ],
        lct: [
          {
            required: true,
            trigger: "blur",
            validator: validateDeviceId("LCT", "LCT值只能输入正整数")
          }
        ]
      }
    }
  },
  methods: {
    getInitialState() {
      const params = this.$route.params
      return new Promise(resolve => {
        getDeviceDetail(params.id).then(res => {
          if (res.code === 200) {
            this.selectRow = res.data
            const { collectType, deviceSeries } = res.data

            if (collectType === 1 || collectType === 2) {
              this.disabledRate = true
            } else {
              this.disabledRate = false
            }

            if (deviceSeries.indexOf("106") > -1) {
              this.showDom = true
            } else {
              this.showDom = false
            }
            resolve(res.data)
          }
        })
      })
    },

    // // 获取设备类型列表
    // getDeviceTypeList() {
    //   getDeviceType().then(res => {
    //     if (res.code === 200) {
    //       const deviceType = this.selectRow.deviceType
    //       const result = res.data.find(t => t.id === deviceType)
    //       if (result) {
    //         getDeviceTypeSeries().then(res => {
    //           const deviceType = res.data.find(t => t.id === result.seriesId)
    //           const { name } = deviceType
    //           if (name && name.indexOf("106") > -1) {
    //           } else {
    //             this.showDom = false
    //           }
    //         })
    //       }
    //     }
    //   })
    // },
    _getcollectorParams() {
      let vm = this
      let params = vm.selectRow.id
      getcollectorParams(params).then(res => {
        if (res.code == 200 && !!res.data) {
          vm.formData = res.data
        }
      })
    },
    saveParams() {
      this.$refs["collectparamsRef"].validate(valid => {
        if (valid) {
          let params = {
            ...this.formData
          }

          params.rate = Number(params.rate)
          params.lct = Number(params.lct)
          params.pt = Number(params.pt)
          params.ct = Number(params.ct)

          params.deviceId = this.selectRow.id
          setcollectorParams(params).then(res => {
            if (res.code == 200) {
              this.$Message.success({
                duration: "2.0",
                content: "保存成功"
              })
              this._getcollectorParams()
            } else {
              this.$Message.error(res.message)
            }
          })
        }
      })
    }
  }
}
</script>
